﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Data;
using System.Data.Sql;
using System.Data.SqlClient;
using APIB;
public partial class IFS_che : System.Web.UI.Page
{
    apibmember apm = new apibmember();

    protected void Page_Load(object sender, EventArgs e)
    {
        if (!IsPostBack)
        {
            binddllmember();
        }
    }

    private void binddllmember()
    {
        SqlConnection con = new SqlConnection(apm.getConn());
        SqlDataAdapter da = new SqlDataAdapter("SELECT firstname + ' ' + lastname as name,memid FROM members", con);

        DataTable dt = new DataTable();

        da.Fill(dt);

        dt.Rows.Add(new Object[] { "---please select---" , 0 });

        DataView view = dt.DefaultView;
        view.Sort = "memid";


        Ddlmember.DataSource = view;
        Ddlmember.DataTextField = "name";
        Ddlmember.DataValueField = "memid";
        Ddlmember.DataBind();

    }

    private void BindData(string memid)
    {
        SqlConnection con = new SqlConnection(apm.getConn());
        SqlDataAdapter da = new SqlDataAdapter(@"select b.mempid,a.memid,a.firstname + ' ' + a.lastname as Fullname,
                                                    b.createdate,b.LeagueID,b.StarBet,c.liveA,c.liveB, 
                                                    CASE 
                                                          WHEN b.Selectteam = 'liveA' THEN c.liveA 
                                                          WHEN b.Selectteam = 'liveB' THEN c.liveB
                                                    END as teamselected
                                                    ,c.score,c.matchTime,d.Mpt_Cal_Point,d.Mpt_Cal_Star,b.MempID,b.Current_PriceHandicap,b.Current_TeamHandicap
                                                    from members a 
                                                    inner join Members_play b on a.memid = b.memid
                                                    inner join match c on b.matchid = c.id  
                                                    inner join Members_point_tran d on b.MempID = d.RefID
                                                    where 1 = 1 and isdone = 0 and a.memid = " + memid, con);

        DataTable dt = new DataTable();

        da.Fill(dt);
        GridView1.DataSource = dt;
        GridView1.DataBind();
    }


    protected void GridView1_RowEditing(object sender, GridViewEditEventArgs e)
    {
        GridView1.EditIndex = e.NewEditIndex;
        BindData(Ddlmember.SelectedValue);
    }
    protected void GridView1_RowCancelingEdit(object sender, GridViewCancelEditEventArgs e)
    {
        GridView1.EditIndex = -1;
        BindData(Ddlmember.SelectedValue);
    }
    protected void GridView1_RowUpdating(object sender, GridViewUpdateEventArgs e)
    {

        //if (((LinkButton)GridView1.Rows[0].Cells[0].Controls[0]).Text == "Insert")
        //{
        //    SqlConnection con = new SqlConnection(apm.getConn());
        //    SqlCommand cmd = new SqlCommand();
        //    cmd.CommandText = "INSERT INTO quest_categories(cat_name) VALUES(@cat_name)";
        //    cmd.Parameters.Add("@cat_name", SqlDbType.VarChar).Value = ((TextBox)GridView1.Rows[0].Cells[2].Controls[0]).Text;

        //    cmd.Connection = con;
        //    con.Open();
        //    cmd.ExecuteNonQuery();
        //    con.Close();
        //}
        //else
        //{
        //    SqlConnection con = new SqlConnection(apm.getConn());
        //    SqlCommand cmd = new SqlCommand();
        //    cmd.CommandText = "UPDATE quest_categories SET cat_name=@cat_name WHERE cat_id=@cat_id";
        //    cmd.Parameters.Add("@cat_name", SqlDbType.VarChar).Value = ((TextBox)GridView1.Rows[e.RowIndex].Cells[2].Controls[0]).Text;
        //    cmd.Parameters.Add("@cat_id", SqlDbType.Int).Value = Convert.ToInt32(GridView1.Rows[e.RowIndex].Cells[1].Text);
        //    cmd.Connection = con;
        //    con.Open();
        //    cmd.ExecuteNonQuery();
        //    con.Close();

        //}

        try
        {
            SqlConnection con = new SqlConnection(apm.getConn());
            SqlCommand cmd = new SqlCommand();

            cmd.CommandText = "UPDATE Members_play SET Selectteam=@Selectteam WHERE MempID=@MempID ";

            //cmd.Parameters.Add("@MempID", SqlDbType.Int).Value = Convert.ToInt32(GridView1.Rows[e.RowIndex]..Cells[2].Text);
            HiddenField hdfmempid = (HiddenField)GridView1.Rows[e.RowIndex].FindControl("hdfmempid");
            DropDownList ddlteam = (DropDownList)GridView1.Rows[e.RowIndex].FindControl("ddlteam");

            cmd.Parameters.Add("@MempID", SqlDbType.BigInt).Value = Convert.ToInt64(hdfmempid.Value); 
            cmd.Parameters.Add("@Selectteam", SqlDbType.VarChar).Value = ddlteam.SelectedValue;

            //((DropDownList)GridView1.Rows[e.RowIndex].Cells[2].Controls[0]).SelectedValue;

            cmd.Connection = con;
            con.Open();
            cmd.ExecuteNonQuery();
            con.Close();

        }
        catch
        { 
        
        }
       
          

        GridView1.EditIndex = -1;
        BindData(Ddlmember.SelectedValue);
    }

    protected void btnAdd_Click(object sender, EventArgs e)
    {
        SqlConnection con = new SqlConnection(apm.getConn());
        SqlDataAdapter da = new SqlDataAdapter("SELECT cat_id, cat_name FROM quest_categories", con);
        DataTable dt = new DataTable();
        da.Fill(dt);

        // Here we'll add a blank row to the returned DataTable
        DataRow dr = dt.NewRow();
        dt.Rows.InsertAt(dr, 0);
        //Creating the first row of GridView to be Editable
        GridView1.EditIndex = 0;
        GridView1.DataSource = dt;
        GridView1.DataBind();
        //Changing the Text for Inserting a New Record
        ((LinkButton)GridView1.Rows[0].Cells[0].Controls[0]).Text = "Insert";

    }
    protected void GridView1_RowDeleting(object sender, GridViewDeleteEventArgs e)
    {
        SqlConnection con = new SqlConnection(apm.getConn());
        SqlCommand cmd = new SqlCommand();
        cmd.CommandText = "DELETE FROM quest_categories WHERE cat_id=@cat_id";
        cmd.Parameters.Add("@cat_id", SqlDbType.Int).Value = Convert.ToInt32(GridView1.Rows[e.RowIndex].Cells[1].Text);

        cmd.Connection = con;
        con.Open();
        cmd.ExecuteNonQuery();
        con.Close();

        BindData(Ddlmember.SelectedValue);

    } 
    protected void Ddlmember_SelectedIndexChanged(Object sender, EventArgs e)
    {
        BindData(Ddlmember.SelectedValue);
    }
}